﻿#include "tw_bt_main.h"
#include "lvgl.h"
#include "com_api.h"
#include "nav.h"
#include "public_api.h"
#include "rope_skipping/rope_blue.h"

#define bt_log(fmt, ...) printf("[BT][%d]%s:"fmt"\r\n", __LINE__, __func__, ##__VA_ARGS__)

static int counts = 0;
static int timeSec = 0;
static int isOver = 0;

static void moni_jump_timer_start(int cmd, int counts);



#ifdef UI_MONITOR
static bt_opencallback rope_bt_open_cb;
static void bt_open_success(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_SHORT_CLICKED)
    {
        lv_obj_t *btnf = lv_event_get_user_data(e);
        lv_obj_t *btns = lv_event_get_target(e);

        lv_obj_del(btnf);
        lv_obj_del(btns);

        rope_bt_open_cb(1);
    }
}


static void bt_open_faild(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_SHORT_CLICKED)
    {
        rope_bt_open_cb(0);
    }
}

void ui_monitor_state_create(lv_obj_t *parent, lv_event_cb_t true_cb, lv_event_cb_t false_cb)
{
    lv_obj_t *btnf = common_btn_create(parent, "失败");
    lv_obj_set_size(btnf, 40, 20);
    lv_obj_add_event_cb(btnf, false_cb, LV_EVENT_SHORT_CLICKED, NULL);
    lv_obj_align(btnf, LV_ALIGN_BOTTOM_RIGHT, 0, 0);

    lv_obj_t *btns = common_btn_create(parent, "成功");
    lv_obj_set_size(btns, 40, 20);
    lv_obj_add_event_cb(btns, true_cb, LV_EVENT_SHORT_CLICKED, btnf);
    lv_obj_align(btns, LV_ALIGN_BOTTOM_LEFT, 0, 0);
}

#endif // UI_MONITOR

void tw_btopen_callback(bt_opencallback open_cb)
{
    ROPE_BLUE_T *pNav = Nav_Get_Param(ACT_ID_ROPE_SKIPPING);
    bt_log("bt open");

    if(pNav == NULL)
    {
        return NULL;
    }

    rope_bt_open_cb = open_cb;
    #ifdef UI_MONITOR
    ui_monitor_state_create(pNav->cont, bt_open_success, bt_open_faild);
    #endif // UI_MONITOR

}

void tw_btjump_change_connect(char *mac,btconnect_callback bt_cb)
{
    bt_log("bt connect");
}

void tw_btjump_mode_set(int mode,int val)
{
    char sndData[32];
    int sndDataLen = 0;
    int cmd = 0;
    uint16_t crc = 0;

    //设置cmd
    if(mode == 1) //计时
        cmd = 0x82;
    else if(mode == 2)
        cmd = 0x81;

    bt_log("mode = %d, val = %d", mode, val);
    memset(sndData, 0, sizeof(sndData));

    sndDataLen = snprintf(sndData, sizeof(sndData), "0203%02x%04x",cmd, val);

    crc = getCrc_16_modbus(sndData, sndDataLen);

    snprintf(sndData, sizeof(sndData), "%s%04x", sndData, crc);

    bt_log("send data:%s", sndData);

    #ifdef UI_MONITOR
    extern void moni_jump_timer_start(int cmd, int counts);
    moni_jump_timer_start(cmd, val);

    isOver = 0;
    timeSec = 0;
    counts = 0;
    #endif
}

void tw_bt_close(void)
{
    bt_log("bt close");
}

int tw_btjump_get_time(void)
{
    return timeSec;
    return tw_get_random()%100;
}

int tw_btjump_get_count(void)
{
    return counts;
    return tw_get_random()%200;
}

int tw_btjump_finished_status(void)
{
    return isOver;
    return !(tw_get_random()%10);
}

int tw_get_bt_connect_status(void)
{
    return 1;
    //return !(tw_get_random()%10);
}

void tw_btjump_stop(void)
{
    moni_jump_timer_start(0, 0);
}

static int initCounts = 0;
void moni_jump_timer(lv_timer_t *timer)
{
    static lv_timer_t *myTimer = NULL;
    int cmd = 0;

    if(timer == NULL) //结束
    {
        bt_log("..");
        counts = 0;
        timeSec = 0;
        if(myTimer)
            lv_timer_del(myTimer);
        myTimer = NULL;
        isOver = 1;
        return;
    }

    myTimer = timer;
    cmd = (int)timer->user_data;
    bt_log("cmd = %x\r\n", cmd);
    switch(cmd)
    {
        case 0x81://计数
            if(counts == 0)counts = initCounts;
            if(counts > 0)
            {
                counts-=(tw_get_random()%10 + 1);
                timeSec++;

                if(counts <= 0)
                {
                    counts = 0;
                    isOver = 1;
                    lv_timer_del(timer);
                    myTimer = NULL;
                }
            }
            break;
        case 0x82://计时
            if(timeSec == 0)timeSec = initCounts;
            if(timeSec > 0)
            {
                timeSec--;
                counts+=(tw_get_random()%10 + 1);

                if(timeSec == 0)
                {
                    isOver = 1;
                    lv_timer_del(timer);
                    myTimer = NULL;
                }
            }
            break;
        default:
            break;
    }
}

void moni_jump_timer_start(int cmd, int counts)
{
    initCounts = counts;

    if(counts == 0)
    {
        moni_jump_timer(NULL);
    }
    else
        lv_timer_create(moni_jump_timer, 100, (void *)cmd);
}



